const app = getApp();
Page({
  data: { createTime: null },
  // 新增列表点击处理
  onListItemClick(e) {
    const itemId = e.currentTarget.dataset.id
    // const itemId = event.currentTarget.dataset.itemId;  // 获取传递的数据
    // 根据 itemId 进行相应的操作，例如跳转到详情页
    wx.navigateTo({
      // url: `/pages/detail/detail?id=${itemId}`  // 假设详情页路径为 /pages/detail/detail
      url: `/pages/d2/d2?id=${itemId}`  // 假设详情页路径为 /pages/detail/detail
    });

    
    // wx.showToast({
    //   title: `跳转到文章 ${id}`,
    //   icon: 'none'
    // })
    // 实际应跳转到对应文章详情页
    // wx.navigateTo({ url: `/pages/detail/detail?id=${id}` })
  },



  onLoad: function(options) {
    const newsId = options.id; // 从上一个页面传递过来的文章ID
    this.getNewsDetail(newsId);
    // this.checkContentHeight();
  },


  getNewsDetail: function(newsId) {
    app.wxRequest('GET', app.globalData.URL + '/news/getNewsDetailsById?newsId='+newsId, "", (res) => {
      this.setData({
        truncatedContent: res.data.newsContent,
        title: res.data.newsTitle,
        showMore: res.data.showMore,
        createTime: res.data.createTime,
        originalContent: res.data.newsContent
      }, () => {
        wx.nextTick(() => {
        //添加标题到主栏
          wx.setNavigationBarTitle({
            title: res.data.newsTitle
          });
          this.checkContentHeight();
          wx.nextTick(() => {
            this.getRecommendNewsList();
          });
        });
      });
    }, (err) => {
      console.log(err.errMsg)
    });


    // // 这里应该调用后端API获取文章详情，以下是模拟数据
    // const article = {
    //   id: id,
    //   title: '文章标题12123',
    //   date: '2023-04-01',
    //   imageUrl: 'https://gateway.juxue.cloud/resmanage/resourcesManage/download?bucketName=1678655958080851969-img&fileName=1678656149325549570/e5282ff7-b599-44ea-bfdb-4de67618fd59.jpg',
    //   content: ''
    // }
    // this.setData({
    //   truncatedContent: article.content,
    //   title: article.title,
    //   originalContent: article.content
    // }, () => {
    //   wx.nextTick(() => {
    //   //添加标题到主栏
    //     wx.setNavigationBarTitle({
    //       title: article.title
    //     });
    //     this.checkContentHeight();
    //   });
    // });
  },
  checkContentHeight() {
    const query = wx.createSelectorQuery().in(this)
    query.select('.content-container').boundingClientRect(rect => {
      this.setData({
        isContentOverflow: rect.height > 50
      })
    }).exec()
  },
  toggleShowMore() {
    this.setData({
      showMore: !this.data.showMore,
      truncatedContent: this.data.showMore ? 
        this.data.originalContent : 
        this.data.originalContent // 实际项目应存储截断版本
    }, this.checkContentHeight)
  },
  getRecommendNewsList: function() {
    app.wxRequest('POST', app.globalData.URL + '/news/getNewsList', {"pageNum":1,"pageSize":100,"createTime":this.data.createTime}, (res) => {
      this.setData({
        recommendNewsList: res.data.rows
      });
    }, (err) => {
      console.log(err.errMsg)
    });
  },
  onShareAppMessage() {
    return {
      title: '闲云文影',
      path: '/pages/index/index'
    }
  },
  
});